<?php
/*
*All rights reserved: Json.
*Links:http://www.liyaoquan.cn.
*Links:http://imarkchina.cn.
*Links:http://www.imarkchina.cn.
*/

/**
 * 博客前台评论模型，负责数据，增，删，改，查
 * 因为没有博客数据没使用数据库，所以没有继承 Model 类
 */
Class IndexpublicModel extends PublicModel{

	/**
	 * 取出博客导航
	 * @return [type] [description]
	 */
	Public function indexTop(){
		$F = loadfile(C('post.listid'));
		uasort($F, 'List_Sort');
		$check = (count($F) >= 8) ? true : false;
		if ($check) {
			$temp = array();
			foreach ($F as $k => $v) {
				$temp[][$k] = $v;
			}
			$listarr = array();
			for ($i=0; $i < 8; $i++) {
				$listarr[] = $temp[$i];
			}
			$listarr = Arr::getArray($listarr);
			$lastarr = array();
			for ($i=9; $i < count($F); $i++) {
				$lastarr[] = $temp[$i];
			}
			$lastarr = Arr::getArray($lastarr);
			$redata['listarr'] = $listarr;
			$redata['lastarr'] = $lastarr;
		} else {
			$redata['listarr'] = $F;
		}
		$redata['check'] = $check;
		return $redata;
	}

	/**
	 * 边栏Ajax评论数据处理
	 */
	Public function RightPingLun(){
		$pinglundir = GetFileList(C('pinglun.id'));
		$pingid = array();
		foreach ($pinglundir as $v) {
			$pingid[] = loadfile(C('pinglun.id').$v);
		}
		if (empty($pingid)) {
			return false;
		}
		$pinglunarr = array();
		foreach ($pingid as $k => $q) {
			if (empty($q)) {
				continue;
			}
			$pinglunarr[] = $q;
		}
		$pinglunarr = Arr::getArray($pinglunarr);
		uasort($pinglunarr, 'Post_Sort');
		$c = 0;
		foreach ($pinglunarr as $k => $v) {
			if ($c == 10) {
				break;
			}
			$pinglunarray[] = unfile(C('pinglun.data').$k.'.Mark');
			$c++;
		}
		if (empty($pinglunarray)) {
			return false;
		}
		foreach ($pinglunarray as $k => $v) {
			$vdate = str_replace('.', '-', $v['date']);
			$pinglunarray[$k]['ptime']  = formDate(strtotime($vdate.$v['time']));
			$pinglunarray[$k]['content'] = $this->msgReplace($v['content']);
			if ($v['username'] == '@i@Mark@China@') {
				$config = $this->blogConfig();
				$pinglunarray[$k]['username'] = $config['user_nick'];
			}
		}
		return $pinglunarray;
	}

	/**
	 * 博客边栏操作
	 * @return [type] [description]
	 */
	Public function indexRight(){
		$C = $this->blogConfig();
		$post = $this->getPost();
		$page = $this->getPage();
		$pagefiles = loadfile(C('page.id'));
		$hot = loadfile(C('hits'));
		uasort($hot, 'hits_sort');
		$fdlinkid = loadfile(C('link.id'));
		uasort($fdlinkid, 'page_sort');
		$postdate = $this->getPost();
		$i = 0;
		$hotpost = array();
		foreach ($hot as $k => $v) {
			if ($i == 6) {
				break;
			}
			if (in_array_case($k, $pagefiles)) {
				continue;
			}
			$hotpost[] = unfile(C('post.postdata').$k.'.Mark');
			$i++;
		}
		if (!empty($fdlinkid)) {
			foreach ($fdlinkid as $k => $v) {
				$fdarray[] = unfile(C('link.data').$k.'.Mark');
			}
		} else {
			$fdarray = array();
		}
		
		$postdate = Arr::getArray($postdate);
		if (!empty($postdate)) {
			foreach ($postdate as $k => $v) {
				$vdate = substr($v['date'], 0, 7);
				$date[$vdate] = substr($v['date'], 0, 7);
			}
		} else {
			$date = array();
		}
		$pagearray = array();
		foreach ($page as $k => $v) {
			if (!$v['index_show']) {
				continue;
			}
			$pagearray[] = $v;
		}
		$redata['check'] = $this->CheckIsOpen();
		$redata['hot'] = $hotpost;
		$redata['date'] = $date;
		$redata['links'] = $fdarray;
		$redata['page'] = $pagearray;
		return $redata;
	}

	/**
	 * 博客标题
	 * @return [type] [description]
	 */
	Public function indexTitle(){
		$C = $this->blogConfig();
		switch (G('get.c')) {
			case 'index':
				$wtitle = $C['site_name'];
				$wkey = $C['site_key'];
				$wdes = $C['site_desc'];
				break;
			case 'list':
				$listfile = loadfile(C('post.listid'));
				$wtitle = $listfile[G('get.lid')]['title'];
				$wkey = $listfile[G('get.lid')]['keyword'];
				$wdes = $listfile[G('get.lid')]['des'];
				break;
			case 'show':
				$cid = G('get.id');
				$fined = loadfile(C('page.id'));
				if (in_array_case($cid, $fined)) {
					$data = unfile(C('page.data').$cid.'.Mark');
					$wtitle = $data['title'];
					$wkey = $data['keyword'];
					$wdes = $data['des'];
				} else {
					$data = unfile(C('post.postdata').$cid.'.Mark');
					$wtitle = $data['title'];
					$wkey = $data['keyword'];
					$wdes = $data['des'];
				}
				break;
			case 'search':
				$getposts = trim(G('post.s'));
				$cid = (!empty($getposts)) ? G('post.s') : G('get.s');
				$wtitle = $cid;
				$wkey = $C['site_key'];
				$wdes = $C['site_desc'];
				break;
			default:
				$wtitle = $C['site_name'];
				$wkey = $C['site_key'];
				$wdes = $C['site_desc'];
				break;
		}
		$redata['title'] = $wtitle;
		$redata['key'] = $wkey;
		$redata['des'] = $wdes;
		return $redata;
	}
}